def set_stage():
""" Sets up the stage for the game """
stage.set_background("soccerfield")
stage.disable_floor()
def add_player():
""" Adds a player to the stage for the user to control """
player = codesters.Sprite("player1")
player.go_to(0, -155)
return player
def main():
""" Sets up the program and calls other functions """
set_stage()
player = add_player()
main()
t = codesters.Teacher()
defs = t.find_block("def")
docstrings = t.find_text('"""')
num_docstrings = len(docstrings)
default_doc = '""" This is a description of what this function does """'.replace(' ','').lower()
ignore_defs = ['set_stage', 'main', 'add_player']
def get_above_def(line_number, def_list):
""" Returns the name of the nearest function and the distance above the given line number """
function_name = ""
distance = line_number - def_list[0][0]
for func in def_list:
if line_number - func[0] <= distance and line_number - func[0] > 0:
function_name = func[1]
distance = line_number - func[0]
return function_name, distance
def find_new_function(ignore_list, def_list):
""" Returns tuples of (line_number, function def) for any functions that aren't specified in ignore list """
return_list = []
for d in def_list:
count = 0
for i in ignore_list:
if i in d[1]:
break
else:
count += 1
if count == len(ignore_list):
return_list.append((d[0],d[1]))
return return_list
try:
new_functions = find_new_function(ignore_defs, defs)
def_name = new_functions[0][1]
except:
def_name = "DNE"
try:
function_above, distance = get_above_def(new_functions[0][0], defs)
except:
function_above = "DNE"
distance = "DNE"
try:
def_indent = t.get_indent_at_line(new_functions[0][0])
except:
def_indent = "DNE"
try:
docstr_text = docstrings[2][1]
docstr_text = docstr_text.replace(' ','').lower()
docstr_line_num = docstrings[2][0]
docstr_indent = t.get_indent_at_line(docstr_line_num)
except:
docstr_text = "DNE"
docstr_line_num == "DNE"
docstr_indent = "DNE"
try:
docstr_def, docstr_distance = get_above_def(docstr_line_num, defs)
except:
docstr_def = "DNE"
docstr_distance = "DNE"
t1 = TestObjective()
t1.add_success("add_ball" in def_name, "Great job!")
t1.add_failure(def_name == "DNE", "Did you add Define Function?")
t1.add_failure("add_ball" not in def_name and def_name != "DNE", "Did you rename the function to add_ball()?")
t2 = TestObjective()
t2.add_success("add_player" in function_above, "Great job!")
t2.add_failure("add_player" not in function_above, "Did you place your new function under add_player() and above main()?")
t2.add_failure(function_above == "", "Did you place add_ball() below add_player()?")
t3 = TestObjective()
t3.add_success(def_indent == 0, "Great job!")
t3.add_failure(def_indent == "DNE", "Did you add Define Function?")
t3.add_failure(def_indent > 0, "Make sure not to indent add_ball() inside another function!")
t4 = TestObjective()
t4.add_success(num_docstrings > 3, "Great job!")
t4.add_failure(num_docstrings <= 3, "Did you add a Docstring under add_ball()?")
t4.add_failure(num_docstrings == 0, "Oops. Did you delete a docstring?")
t5 = TestObjective()
t5.add_success(docstr_text != default_doc and num_docstrings > 3, "Great job!")
t5.add_failure(docstr_text == default_doc, "Did you change the docstring to describe the function?")
t5.add_failure(num_docstrings == 3, "Did you add a Docstring under add_ball()?")
t6 = TestObjective()
t6.add_success(docstr_indent == 4 and num_docstrings > 3, "Great job!")
t6.add_failure(docstr_indent < 4, "Did you indent the docstring inside add_ball()?")
t6.add_failure(docstr_indent > 4, "Oops, did you indent the docstring more than once?")
t6.add_failure(docstr_indent == "DNE", "Did you indent the docstring inside add_ball()?")
t6.add_failure(num_docstrings == 3, "Did you add a Docstring under add_ball()?")
t7 = TestObjective()
t7.add_success("add_ball" in docstr_def and docstr_distance < 5, "Great job!")
t7.add_failure("add_ball" not in docstr_def, "Did you place the docstring under add_ball()?")
t7.add_failure(docstr_distance >= 5, "Make sure the docstring is at the top of the function!")
t7.add_failure(docstr_def == "DNE", "Did you delete a docstring?")
################################################
# Pass test code
# - set pass_required to True if pass is required
# - include tpass before t1 in the test list
pass_required = False
passes = t.find_text("pass")
num_pass_only = 0
for p in passes:
if p[1].lower().replace(' ','') == "pass":
num_pass_only += 1
tpass = TestObjective()
if not pass_required:
tpass.add_success(num_pass_only == 0, "Great job!")
tpass.add_creative(num_pass_only > 0, "Great job! Feel free to delete pass statements now.")
################################################
tester = TestManager()
tester.add_test_list([tpass, t1, t2, t3, t4, t5, t6, t7])
tester.run_tests()
tester.display_first_feedback()